/* vim: set expandtab shiftwidth=4: -*- mode: c++; c-basic-offset: 4; tab-width: 8; indent-tabs-mode: nil -*-
qRPG
An RPG written to test Qt's Graphics View framework
Copyright (C) 2006 Wesley Crossman
Email: wesley@crossmans.net

You can redistribute and/or modify this software under the terms of the GNU
General Public License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this
program; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
Suite 330, Boston, MA 02111-1307 USA */

#include "GameBoard.h"
#include "GameBoardScene.h"
#include <QtGui>

class BoardCharacter;

GameBoard::GameBoard(QWidget *parent) : QGraphicsView(parent) {
    gbscene = new GameBoardScene(this);
    setScene(gbscene);
    setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
}

void GameBoard::mousePressEvent(QMouseEvent *e) {
    QGraphicsView::mousePressEvent(e);
    if (e->button() == Qt::MidButton) {
        last = QPoint();
        viewport()->setCursor(Qt::SizeAllCursor);
    }
}

void GameBoard::mouseReleaseEvent(QMouseEvent *e) {
    QGraphicsView::mouseReleaseEvent(e);
    if (!last.isNull()) {
        viewport()->unsetCursor();
    }
}

void GameBoard::mouseMoveEvent(QMouseEvent *e) {
    QGraphicsView::mouseMoveEvent(e);
    if (e->buttons() & Qt::MidButton) {
        if (!last.isNull()) {
            QPoint delta = e->pos() - last;
            horizontalScrollBar()->setValue(horizontalScrollBar()->value()
                    + (isRightToLeft() ? delta.x() : -delta.x()));
            verticalScrollBar()->setValue(verticalScrollBar()->value()
                    - delta.y());
        }
    last = e->pos();
    }
}

void GameBoard::centerOnX(qreal x, qreal percent) {
    //Function won't operate with matrix rotation/skewing, since it doesn't have the value for y.
    //XXX why is there RTL code in centerOn() in QGraphicsView? I disabled it until I understand it. - WJC
    qreal width = viewport()->width();
    qreal height = viewport()->height();
    QPointF viewPoint = matrix().map(QPointF(x, 0));
    QPointF viewTopLft = matrix().inverted().map(QPointF(horizontalScrollBar()->value(), verticalScrollBar()->value()));
    QPointF viewBtmRgt = matrix().inverted().map(QPointF(horizontalScrollBar()->value()+width, verticalScrollBar()->value()+height));
    qreal calcPercent = (x - viewTopLft.x()) / (viewBtmRgt.x()-viewTopLft.x());
    /*if (isRightToLeft()) {
        qint64 horizontal = 0;
        horizontal += horizontalScrollBar()->minimum();
        horizontal += horizontalScrollBar()->maximum();
        horizontal -= int(viewPoint.x() - width / 2.0);
        horizontalScrollBar()->setValue(horizontal);
    } else {
    */
    if (percent == 0.0 || calcPercent < percent || calcPercent > 1.0-percent) {
        horizontalScrollBar()->setValue(int(viewPoint.x() - width / 2.0));
    }
}

void GameBoard::centerOnY(qreal y, qreal percent) {
    //Function won't operate with matrix rotation/skewing, since it doesn't have the value for x.
    qreal width = viewport()->width();
    qreal height = viewport()->height();
    QPointF viewPoint = matrix().map(QPointF(0, y));
    QPointF tmp;
    tmp = QPointF(horizontalScrollBar()->value(), verticalScrollBar()->value());
    QPointF viewTopLft = matrix().inverted().map(tmp);
    tmp = QPointF(horizontalScrollBar()->value()+width, verticalScrollBar()->value()+height);
    QPointF viewBtmRgt = matrix().inverted().map(tmp);
    qreal calcPercent = (y - viewTopLft.y()) / (viewBtmRgt.y()-viewTopLft.y());
    if (percent == 0.0 || calcPercent < percent || calcPercent > 1.0-percent) {
        verticalScrollBar()->setValue(int(viewPoint.y() - height / 2.0));
    }
}

